package ObjectArray;

/**
 * Created by sunwei on 2017/10/13
 * Time:14:57
 */
public class PersonSolve {
    private Person[] a;
    private int nElems;

    /*
    构造函数
     */
    public PersonSolve(int size) {
        a = new Person[size];
        nElems = 0;
    }
    /*
    添加人
     */
    public void insertPerson(String firstName,String lastName,int age){
        a[nElems] = new Person(firstName,lastName,age);
        nElems++;
    }

    /*
    删除人的信息
     */
    public boolean delete(String searchName){
        int j;
        for(j= 0;  j<nElems ; j++) {
            if (a[j].getLastName().equals(searchName)) {
                  break;
            }
        }
        if (j==nElems) {
              return false;
        }else {
            for(int k = j; k <nElems ; k++) {
                a[k] = a[k+1];
            }
            nElems--;
            return true;
        }
    }

    /*
    遍历显示人的信息
     */
    public void displayPerson(){
        for(int i = 0; i <nElems ; i++) {
            a[i].displayPerson();
        }
    }
    /*
    查找相应的人的信息
     */
    public Person find(String searchName){
        int j;
        //循环查找这个人
        for(j = 0;  j<nElems ; j++) {
            if (a[j].getLastName().equals(searchName)) {
                  break;
            }
        }
        if (j==nElems) {
              return null;
        }  else {
            return a[j];
        }
    }

}
